package com.shenmazong.servereduresume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.servereduresume.dto.TbSrwp;
import com.shenmazong.servereduresume.service.TbSrwpService;
import com.shenmazong.servereduresume.mapper.TbSrwpMapper;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import com.shenmazong.vo.ListParamVo;
import com.shenmazong.vo.SrwpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 *
 */
@Service
public class TbSrwpServiceImpl extends ServiceImpl<TbSrwpMapper, TbSrwp>
implements TbSrwpService{

    @Autowired
    private TbSrwpMapper tbSrwpMapper;

    @Override
    public ResultResponse findAllSrwpAdmin(ListParamVo listParamVo) {
        Page<SrwpVo> page = new Page<>(listParamVo.getCurrent(), listParamVo.getSize());
        page = tbSrwpMapper.findAllSrwpAdmin(page, listParamVo.getParamMap());
        return ResultResponse.SUCCESS(page);
    }

    @Override
    public ResultResponse findAllSrwpStudent(ListParamVo listParamVo) {
        Page<SrwpVo> page = new Page<>(listParamVo.getCurrent(), listParamVo.getSize());
        page = tbSrwpMapper.findAllSrwpStudent(page, listParamVo.getParamMap());
        return ResultResponse.SUCCESS(page);
    }

    @Override
    public ResultResponse saveSrwp(TbSrwp tbSrwp) {
        try {
            tbSrwpMapper.insert(tbSrwp);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateSrwp(TbSrwp tbSrwp) {
        try {
            TbSrwp srwp = tbSrwpMapper.selectById(tbSrwp.getSrwpId());
            srwp.setUpdateTime(new Date());
            if (!StringUtils.isEmpty(tbSrwp.getSrwpName())){
                srwp.setSrwpName(tbSrwp.getSrwpName());
            }
            if (!StringUtils.isEmpty(tbSrwp.getSrwpUrl())){
                srwp.setSrwpUrl(tbSrwp.getSrwpUrl());
            }
            if (!StringUtils.isEmpty(tbSrwp.getSrwpRecommend())){
                srwp.setSrwpRecommend(tbSrwp.getSrwpRecommend());
            }
            tbSrwpMapper.updateById(srwp);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse deleteSrwp(BatchChooseIdVo batchChooseIdVo) {
        if (batchChooseIdVo.getIds().size()==0){
            return ResultResponse.FAILED("至少删除一个哟");
        }
        try {
            for (Long id : batchChooseIdVo.getIds()) {
                TbSrwp tbSrwp = tbSrwpMapper.selectById(id);
                tbSrwp.setDeleteFlag(true);
                tbSrwpMapper.updateById(tbSrwp);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse auditSrwp(BatchChooseIdVo batchChooseIdVo) {
        if (batchChooseIdVo.getIds().size()==0){
            return ResultResponse.FAILED("至少审核一个哟");
        }
        try {
            Byte flag = (Byte) batchChooseIdVo.getParamMap().get("audit");
            for (Long id : batchChooseIdVo.getIds()) {
                TbSrwp tbSrwp = tbSrwpMapper.selectById(id);
                tbSrwp.setSrwpAudit(flag);
                tbSrwpMapper.updateById(tbSrwp);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }
}




